from fastapi import APIRouter
from pydantic import BaseModel
from finance.python.services.auth_service import AuthService    # 绝对导入

# 创建路由器
login_router = APIRouter(
    prefix="/api",
    tags=["login"]
)


# 规定数据类型
class LoginRequest(BaseModel):
    login_type: str  # username 或 phone
    username: str = None
    password: str = None
    phone: str = None
    verify_code: str = None


@login_router.post(path="/login", summary="登录")
async def login(login_data: LoginRequest):
    """
    登录
    :param login_data: 登录请求数据
    :return: 登录结果
    """
    auth_service = AuthService()
    if login_data.login_type == "username":
        return await auth_service.authenticate_user(
            login_data.username,
            login_data.password
        )
    elif login_data.login_type == "phone":
        return await auth_service.authenticate_user_by_phone(
            login_data.phone,
            login_data.verify_code
        )
    else:
        return {"code": 1, "message": "不支持的登录方式"}


class VerifyCodeRequest(BaseModel):
    phone: str

@login_router.post(path="/verify_code", summary="生成验证码")
async def verify_code(data: VerifyCodeRequest):
    """
    生成验证码
    :param data: 验证码请求数据
    :return: 验证码结果
    """
    auth_service = AuthService()
    return await auth_service.verify_code(data.phone)
